using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Common.NotifyEvent;
using SExperiment;

namespace SWellPlate
{
	public class WellPlate : NotifyPropertyChanged
	{
		private ObservableCollection<WellRow> _WellRows = new ObservableCollection<WellRow>();

		private ObservableCollection<WellCell> _WellCells = new ObservableCollection<WellCell>();

		private ObservableCollection<ChartData> _WellTargets = new ObservableCollection<ChartData>();

		public ObservableCollection<WellRow> WellRows
		{
			get
			{
				return _WellRows;
			}
			set
			{
				if (_WellRows != value)
				{
					_WellRows = value;
					OnPropertyChanged("WellRows");
				}
			}
		}

		public ObservableCollection<WellCell> WellCells
		{
			get
			{
				return _WellCells;
			}
			set
			{
				if (_WellCells != value)
				{
					_WellCells = value;
					OnPropertyChanged("WellCells");
				}
			}
		}

		public ObservableCollection<ChartData> WellTargets
		{
			get
			{
				return _WellTargets;
			}
			set
			{
				if (_WellTargets != value)
				{
					_WellTargets = value;
					OnPropertyChanged("WellTargets");
				}
			}
		}

		internal void RefreshWellRow(int ColumnCount, List<Well> Wells)
		{
			_WellRows.Clear();
			if (Wells == null || ColumnCount <= 0)
			{
				return;
			}
			for (int i = 0; i < Wells.Count / ColumnCount; i++)
			{
				WellRow wellRow = new WellRow();
				for (int j = 0; j < ColumnCount; j++)
				{
					Well well = Wells[i * ColumnCount + j];
					if (well != null)
					{
						WellCell wellCell = new WellCell(i, j);
						wellCell.PropertyChanged += WellCell_PropertyChanged;
						wellCell.Well = well;
						wellCell.Well.CellName = wellCell.RowName + wellCell.ColumnName;
						WellCells.Add(wellCell);
						wellRow.Items.Add(wellCell);
					}
				}
				wellRow.Name = wellRow.Items[0].RowName;
				_WellRows.Add(wellRow);
			}
		}

		private void WellCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			WellCell wellCell = sender as WellCell;
			if (wellCell == null || !(e.PropertyName == "ChartData"))
			{
				return;
			}
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx == null)
			{
				return;
			}
			if (propertyChangedEventArgsEx.OldValue != null && propertyChangedEventArgsEx.OldValue is List<ChartData>)
			{
				foreach (ChartData item in (List<ChartData>)propertyChangedEventArgsEx.OldValue)
				{
					_WellTargets.Remove(item);
				}
			}
			if (propertyChangedEventArgsEx.NewValue == null || !(propertyChangedEventArgsEx.NewValue is List<ChartData>))
			{
				return;
			}
			foreach (ChartData item2 in (List<ChartData>)propertyChangedEventArgsEx.NewValue)
			{
				_WellTargets.Add(item2);
			}
		}

		internal int GetColumnCount()
		{
			if (_WellRows.Count > 0 && _WellRows[0].Items.Count > 0)
			{
				return _WellRows[0].Items.Count;
			}
			return 0;
		}

		internal string GetColumnHeader(int index)
		{
			if (_WellRows.Count > 0 && index < _WellRows[0].Items.Count)
			{
				return _WellRows[0].Items[index].ColumnName;
			}
			return null;
		}
	}
}
